package org.dawfinn.droiditarian;

import android.app.NotificationManager;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;

public class TabWidget extends TabActivity implements LocationListener {

	private static final float MIN_DISTANCE = (float) 100.0;
	private static final int MIN_TIME = 1000 * 60 * 60;
	private static final String RECENT_TAG = "recent";
	private static final String FAV_TAG = "favorite";
	private static final String MAP_TAG = "map";
	
	private LocationManager lm;
	private final BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancelAll();
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);

	    Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, RecentDeeds.class);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec(RECENT_TAG).setIndicator(getString(R.string.recent_tab_text),
	                      res.getDrawable(R.drawable.ic_tab_recent))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, FavoritedDeeds.class);
	    spec = tabHost.newTabSpec(FAV_TAG).setIndicator(getString(R.string.favorited_tab_text),
	                      res.getDrawable(R.drawable.ic_tab_fav))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, DeedMap.class);
	    spec = tabHost.newTabSpec(MAP_TAG).setIndicator(getString(R.string.map_tab_text),
	                      res.getDrawable(R.drawable.ic_tab_map))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    tabHost.setCurrentTabByTag("recent");

		lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		// Cancel notification if there was one
		((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancel(DeedsUpdaterService.NOTIFICATION_ID);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancelAll(); //(DeedsUpdaterService.NOTIFICATION_ID);
		IntentFilter filter = new IntentFilter(DeedsUpdaterService.DEEDS_UPDATED);
		registerReceiver(receiver, filter);
		
		if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
		} else if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
			lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		unregisterReceiver(receiver);
		lm.removeUpdates(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem help = menu.add(getString(R.string.help_menu_item));
		help.setIntent(new Intent(this, Help.class));
		MenuItem share = menu.add(getString(R.string.share_deed_menu_item));
		share.setIntent(new Intent(Intent.ACTION_VIEW,
				Uri.parse(getString(R.string.share_deeds_form_url))));
		// TODO: uncomment when we have a paid app version
		// MenuItem paid = menu.add("Paid App");
		// paid.setOnMenuItemClickListener(new FinishActivityWithIntent(this,
		// new Intent(this, PaidApp.class)));
		return true;
	}

	// We don't care about the location updates, just that the location is
	// up-to-date
	public void onLocationChanged(Location arg0) {
	}

	public void onProviderDisabled(String arg0) {
	}

	public void onProviderEnabled(String arg0) {
	}

	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
	}
}
